<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\Article;
use app\index\model\Collect;
use app\index\model\Comment;
use app\index\model\Follow;
use app\index\model\User;
use think\facade\Db;
use think\facade\View;

class Profile extends BaseController
{

    public function index()
    {
        $this->logs('个人简介文章');
        $uid = input('uid');
        if (empty($uid)) return view('error/404', ['msg' => 'uid不能为空']);
        $userData = User::where('id', $uid);
        if ($userData->findOrEmpty()->isEmpty()) {
            $msg = '不存在该用户数据';
            return view('error/404', ['data' => $msg]);
        }
        $articleList = (new Article())->list(['user_id' => $uid]);
        $page = $articleList['list']->render();
        $userData = (new User())->profile($uid);
        return view('index', ['userData' => $userData, 'artList' => $articleList, 'page' => $page]);
    }
    /**
     * 由于悬赏模式评论查看个人首页没有传递文章id，导致可以查看Ta隐藏的评论，先删除
     */
    // public function comment()
    // {
    //     $this->logs('个人简介评论');
    //     $uid = input('uid');
    //     if (empty($uid)) return view('error/404', ['msg' => 'uid不能为空']);
    //     $userData = User::where('id', $uid);
    //     if ($userData->findOrEmpty()->isEmpty()) {
    //         $msg = '不存在该用户数据';
    //         return view('error/404', ['data' => $msg]);
    //     }
    //     $commentList = (new Comment())->list(['user_id' => $uid]);
    //     $page = $commentList['list']->render();
    //     $userData = (new User())->profile($uid);
    //     return view('comment', ['userData' => $userData, 'commentList' => $commentList, 'page' => $page]);
    // }
    public function collect()
    {
        $this->logs('个人简介收藏');
        $uid = input('uid');
        if (empty($uid)) return view('error/404', ['msg' => 'uid不能为空']);
        $userData = User::where('id', $uid);
        if ($userData->findOrEmpty()->isEmpty()) {
            $msg = '不存在该用户数据';
            return view('error/404', ['data' => $msg]);
        }
        $collectList = Collect::list($uid);
        $page = $collectList['list']->render();
        $userData = (new User())->profile($uid);
        return view('collect', ['userData' => $userData, 'collectList' => $collectList, 'page' => $page]);
    }
    public function follow()
    {
        $this->logs('用户关注');
        $uid = input('uid');
        if (empty($uid)) return view('error/404', ['msg' => 'uid不能为空']);
        $userData = User::where('id', $uid);
        if ($userData->findOrEmpty()->isEmpty()) {
            $msg = '不存在该用户数据';
            return view('error/404', ['data' => $msg]);
        }
        $follow = Follow::list($uid);
        $page = $follow->render();
        $userData = (new User())->profile($uid);
        return view('follow', ['userData' => $userData, 'list' => $follow, 'page' => $page]);
    }
    /**
     * @type 0 关注 1 取消关注
     */
    public function doFollow()
    {
        // delete_time 0 关注 非0 取消关注
        $followId = input('uid');
        if (empty($followId)) return error('参数UID不能为空');
        if($followId == $this->user['user_id']) return error('不能关注自己哦！');
        $hasFollow = Db::name('follow')->where(['user_id' => $this->user['user_id'], 'follow_user_id' => $followId]);
        // 取消关注
        if ($hasFollow->find() && $hasFollow->find()['delete_time'] == 0) {
            $res = Db::name('follow')->where(['user_id' => $this->user['user_id'], 'follow_user_id' => $followId])->update(['delete_time' => time(),'create_time' => 0]);
        } 
        // 关注
        else if($hasFollow->find() && $hasFollow->find()['delete_time'] != 0){
            $res = Db::name('follow')->where(['user_id' => $this->user['user_id'], 'follow_user_id' => $followId])->update(['delete_time' => 0,'create_time' => time()]);
        }
        else {
            $res = $hasFollow->insert(['user_id' => $this->user['user_id'], 'follow_user_id' => $followId,'delete_time' => 0,'create_time' => time()]);
        }
        if ($res) return success();
        return error();
    }
}
